//
//  JHRegisterViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/8.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHRegisterViewController.h"
#import "WKVerCodeImageView.h"
#import "JHLoginViewController.h"

@interface JHRegisterViewController ()
@property (strong, nonatomic) IBOutlet UITextField *phoneTextField;
@property (strong, nonatomic) IBOutlet UITextField *pawTextField;
@property (strong, nonatomic) IBOutlet UITextField *surePawTextField;
@property (strong, nonatomic) IBOutlet UITextField *codeTextField;
@property (strong, nonatomic) IBOutlet UIView *codeView;
@property (strong, nonatomic) IBOutlet UIButton *changeCodeBtn;
@property (strong, nonatomic) IBOutlet UIButton *registerBtn;
@property (nonatomic, strong) WKVerCodeImageView *codeImageView;
@property (nonatomic,strong) NSString *codeStr;

@end

@implementation JHRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title=@"注册";
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [_codeImageView freshVerCode];

}

- (void)initUI
{
    self.registerBtn.backgroundColor=JKMainColor;
    JKWeakSelf
    self.codeImageView = [[WKVerCodeImageView alloc] initWithFrame:self.codeView.bounds];
    _codeImageView.bolck = ^(NSString *imageCodeStr){//看情况是否需要
        NSLog(@"imageCodeStr = %@",imageCodeStr);
        weakSelf.codeStr=[NSString stringWithFormat:@"%@", imageCodeStr];
    };
    //是否需要斜体
    _codeImageView.isRotation = NO;
    [self.codeView addSubview:self.codeImageView];
}

#pragma mark   刷新验证码

- (IBAction)changeCodeBtnDidClick:(UIButton *)sender {
     [self.codeImageView freshVerCode];
}

#pragma mark---  注册
- (IBAction)registerBtnDidClick:(UIButton *)sender {
    
    if (self.phoneTextField.text.length==0) {
         return [DJToast showCenterToast:@"手机号码不能为空"];
    }else if (self.phoneTextField.text.length!=11){
         return [DJToast showCenterToast:@"手机号码输入错误"];
    }else if (self.pawTextField.text.length==0){
         return [DJToast showCenterToast:@"密码不能为空"];
    }else if ([DJTool justPassword:self.pawTextField.text]==NO)
    {
         return [DJToast showCenterToast:@"密码只能输入数字和字母"];
    }else if (self.surePawTextField.text.length==0){
       return [DJToast showCenterToast:@"确认密码不能为空"];
    }else if ([DJTool justPassword:self.surePawTextField.text]==NO)
    {
        return [DJToast showCenterToast:@"确认密码只能输入数字和字母"];
    }else if ([self.pawTextField.text isEqualToString:self.surePawTextField.text]==NO){
        return [DJToast showCenterToast:@"确认密码只能输入数字和字母"];
    }
    
    BOOL result1 =  [self.codeTextField.text compare:self.codeStr options:NSCaseInsensitiveSearch | NSNumericSearch]==NSOrderedSame;
    if (result1) {
        NSLog(@"succes");
    }else{
        return [DJToast showCenterToast:@"验证码输入错误"];
    }
    
    NSMutableDictionary *parmes=[NSMutableDictionary dictionary];
    parmes[@"username"]=self.phoneTextField.text;
    parmes[@"password"]=self.pawTextField.text;
    parmes[@"regcode"]=self.codeTextField.text;
    NSString *urlStr=[BaseUrl stringByAppendingString:users_Register];
    [DJHttpTool postWithURL:urlStr params:parmes success:^(id json) {
        BOOL isSuccess=json[@"success"];
        if (isSuccess==YES) {
            [DJToast showCenterToast:@"注册成功"];
            [DJTool afterTime:1.5 Action:^{
                JHLoginViewController *vc=[[JHLoginViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }];
        }else{
            [DJToast showCenterToast:json[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];

    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
